<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="生产线名称" prop="workshopName">
        <el-input
          v-model="queryParams.workshopName"
          placeholder="请输入生产线名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:workshop:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:workshop:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:workshop:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:workshop:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-main>
      <el-table show-summary v-loading="loading" :data="workshopList" @selection-change="handleSelectionChange" >
        <el-table-column type="selection" width="70" align="center" />
        <!-- <el-table-column label="生产线ID" align="center" prop="workshopId" width="80" /> -->
        <el-table-column label="生产线名称" align="center" prop="workshopName" width="120" />
        <el-table-column label="属性" align="center" prop="workshopPropertyBean.dictLabel" width="80"/>
        <el-table-column label="归属公司" align="center" prop="dictCodeBean.dictLabel" />
        <el-table-column label="产地" align="center" prop="workshopChandi" width="70" />
        <el-table-column label="排序" align="center" prop="workshopOrder" width="70" sortable />
        <el-table-column label="维护人" align="center" prop="sysUser.nickName" width="70" />
        <el-table-column label="维护时间" align="center" prop="workshopTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.workshopTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:workshop:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:workshop:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-main>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改生产线对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="生产线名称" prop="workshopName">
          <el-input
            v-model="form.workshopName"
            placeholder="请输入生产线名称"
          />
        </el-form-item>

        <el-form-item label="属性" prop="workshopProperty">
          <el-select
            v-model="form.workshopProperty"
            placeholder="请选择生产线属性" style="width:100%"
          >
            <el-option
              v-for="item in workshopPropertyOptions"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所属公司" prop="dictCode">
          <el-select v-model="form.dictCode" placeholder="请选择公司"  style="width:100%">
            <el-option
              v-for="item in dictCodeOptions"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="产地" prop="workshopChandi" >
          <el-input v-model="form.workshopChandi" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="排序" prop="workshopOrder">
          <el-input-number v-model="form.workshopOrder" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWorkshop,
  getWorkshop,
  delWorkshop,
  addWorkshop,
  updateWorkshop,
  exportWorkshop,
  verifyPubWorkshop,
} from "@/api/erp/system/workshop";
import { getUserProfile } from "@/api/system/user";

export default {
  name: "Workshop",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产线表格数据
      workshopList: [],
      // 弹出层标题
      title: "生产线维护",
      // 是否显示弹出层
      open: false,
      // 生产线属性数据
      workshopPropertyOptions: [],
      // 公司名称数据
      dictCodeOptions: [],
      userId : 0,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        workshopName: null,
        // workshopProperty: null,
        // dictCode: null,
        // workshopChandi: null,
        // workshopOrder: null,
        // workshopIsdel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workshopName: [
          { required: true, message: "生产线名称不能为空", trigger: "blur" },
        ],
        workshopProperty: [
          { required: true, message: "生产线属性不能为空", trigger: "blur" },
        ],
        dictCode: [
          { required: true, message: "所属公司不能为空", trigger: "blur" },
        ],
        workshopChandi: [
          { required: true, message: "产地不能为空", trigger: "blur" },
        ],
        workshopOrder: [
          { required: true, message: "排序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("workshop_property").then((response) => {
      this.workshopPropertyOptions = response.data;
    });
    this.getDicts("erp_changqu").then((response) => {
      this.dictCodeOptions = response.data;
    });
    getUserProfile().then(response => {
        this.userId = response.data.userId;
    });
  },
  methods: {
    /** 查询生产线列表 */
    getList() {
      this.loading = true;
      listWorkshop(this.queryParams).then((response) => {
        this.workshopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 生产线属性 酸洗、轧硬、镀锌、彩涂，其他 关联：sys_dict_data字典翻译
    workshopPropertyFormat(row, column) {
      return this.selectDictLabel(
        this.workshopPropertyOptions,
        row.workshopProperty
      );
    },
    // 公司名称，属于哪个公司，关联：sys_dict_data字典翻译
    dictCodeFormat(row, column) {
      return this.selectDictLabel(this.dictCodeOptions, row.dictCode);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workshopId: null,
        workshopName: null,
        workshopProperty: null,
        dictCode: null,
        workshopChandi: null,
        workshopOrder: null,
        workshopIsdel: null,
      };
      this.resetForm("form");
      this.form.userId = this.userId;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.workshopId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产线";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workshopId = row.workshopId || this.ids;
      getWorkshop(workshopId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产线";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.workshopId != null) {
            updateWorkshop(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkshop(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workshopIds = row.workshopId || this.ids;
      this.$confirm(
        '是否确认删除生产线名称为"' + row.workshopName + '"的数据项吗?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delWorkshop(workshopIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有生产线数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportWorkshop(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
